﻿using System;
using System.Linq;
using Apps.IDAL.Flow;
using Apps.Models;
using System.Data;
using System.Linq.Expressions;

namespace Apps.DAL.Flow
{
    public partial class Flow_StepRepository
    {
        public int Delete(string id)
        {

            Flow_Step entity = Context.Flow_Step.SingleOrDefault(a => a.Id == id);
                if (entity != null)
                {
                    IQueryable<Flow_StepRule> collection = from f in Context.Flow_StepRule
                                                           where f.StepId==id
                                                           select f;
                    foreach (var deleteItem in collection)
                    {
                        Context.Flow_StepRule.Remove(deleteItem);
                    }
                    Context.Flow_Step.Remove(entity);
                }
                return this.SaveChanges();
        }
         

        public IQueryable<Flow_Step> GetHisStepBycontentId(string contentId)
        {
            var intstepno = (from c in Context.Flow_FormContentStepCheck
                          where c.ContentId == contentId & c.State == 2  
                          select c.STEPNO).Single();

            IQueryable<Flow_Step> list = from b in Context.Flow_Step
                                         join a in Context.Flow_FormContentStepCheck
                                         on b.Id equals a.StepId
                                         where a.ContentId == contentId & a.State == 1 & a.StateFlag == true & a.STEPNO < intstepno
                                         orderby b.Sort descending
                                         select b; 
            return list;
        }

        public Flow_Step GetNextStepBycontentId(string curStepId,string contentId)
        {
            Flow_Step nextstep = (from a in Context.Flow_FormContentStepCheck
                                         join b in Context.Flow_Step
                                         on a.StepId equals b.Id
                                         where a.ContentId == contentId & a.State == 2 & a.StateFlag == false & a.Id != curStepId
                                         orderby b.Sort
                                         select b).FirstOrDefault();
            return nextstep;
        }

        public Flow_Step GetNextStepByCurStepId(string curStepId,string formid)
        {
            Flow_Step nextstep;
            if (string.IsNullOrEmpty(curStepId))
            {
                nextstep = (from a in Context.Flow_Step 
                            where a.FormId == formid  
                            orderby a.Sort
                            select a).FirstOrDefault();
            }
            else
            {
                nextstep = (from a in Context.Flow_Step
                            join b in Context.Flow_Step
                            on a.FormId equals b.FormId
                            where b.Id == curStepId & b.FormId == formid & a.Sort > b.Sort
                            orderby a.Sort
                            select a).FirstOrDefault();
            }
            return nextstep;
        }
    }
}
